<?php
/**
 * Created by PhpStorm.
 * User: feng
 * Date: 2016/12/19
 * Time: 上午11:12
 */

function buildPassword($password, $rand_str)
{
    $str = $password . $rand_str;
    return md5(sha1($str));
}

function getRandStr($length = 4)
{
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $str = '';
    for ($i = 0; $i < $length; $i++) {
        $str .= $chars[mt_rand(0, strlen($chars) - 1)];
    }
    return $str;
}

function sessionOpen()
{
    if (!isset($_SESSION)) {
        session_start();
    }
}

function arrToStrById($arr)
{
    $size = sizeof($arr);
    $str = '';
    for ($i = 0; $i < $size; $i++) {
        if ($i == $size - 1) {
            //最后一个
            $str .= $arr[$i]['id'];
        } else {
            $str .= $arr[$i]['id'] . ',';
        }
    }
    return $str;
}

function arrToStr($arr)
{
    $size = sizeof($arr);
    $str = '';
    for ($i = 0; $i < $size; $i++) {
        if ($i == $size - 1) {
            //最后一个
            $str .= $arr[$i];
        } else {
            $str .= $arr[$i] . ',';
        }
    }
    return $str;
}

function pregMatchHorizontalLine($str)
{
    //((?!win).)* 表示匹配除单词win的其它所有字符
    $arr = array();
    try {
//        preg_match_all('/(?<=<span style="text-decoration: line-through;">)([^<])*(?=<\/span>)/s', $str, $out);
        preg_match_all('/(?<=<span style="text-decoration: line-through;">)((?!<\/span>).)*(?=<\/span>)/s', $str, $out);

        $arr = $out[0];
    } catch (Exception $e) {
        exit($e->getMessage());
    }
    return $arr;
}

function calculateIconvStrlenByArr($arr)
{
    $sum = 0;
    foreach ($arr as $k => $v) {
        if (is_string($v)) {
            $sum += iconv_strlen($v, 'utf-8');
        }
    }
    return $sum;
}

/**
 * 等级计算
 * 等级升级需要:
 * 1 500    500
 * 2 1000   1500
 * 3 2000   3500
 * 4 3500   7000
 * 5 5500   12500
 * 6 8000   20500
 * 7 11000  31500
 * 8 14500  46000
 * 9 18500  64500
 */

function calculateLevel($jingyan)
{

    $level_arr = array(
        0 => array(0, 500),
        1 => array(500, 1000),
        2 => array(1000, 2000),
        3 => array(2000, 3500),
        4 => array(3500, 5500),
        5 => array(5500, 8000),
        6 => array(8000, 11000),
        7 => array(11000, 14500),
        8 => array(14500, 18500),
        9 => array(18500, 9999999999999),
    );
    if (empty($jingyan)) {
        $level = 0;
        return array('level' => $level, 'next' => $level_arr[($level + 1) > 9 ? $level : ($level + 1)]);
    }
    $level = 0;
    foreach ($level_arr as $k => $v) {
        if ($v[0] <= $jingyan && $jingyan < $v[1]) {
            $level = $k;
            break;
        }
    }
    return array('level' => $level, 'next' => $level_arr[($level + 1) > 9 ? $level : ($level + 1)]);
}

function calculateJingYan($score, $text_number)
{
    switch ($score) {
        case 0:
            return array('is_add' => 2, 'jingyan' => 0);
            break;
        case 1:
            return array('is_add' => 1, 'jingyan' => ceil(0.05 * $text_number));
            break;
        case 2:
            return array('is_add' => 1, 'jingyan' => ceil(0.15 * $text_number));
            break;
        case 3:
            return array('is_add' => 1, 'jingyan' => ceil(0.35 * $text_number));
            break;
        case 4:
            return array('is_add' => 1, 'jingyan' => ceil(0.65 * $text_number));
            break;
        case 5:
            return array('is_add' => 1, 'jingyan' => ceil(1 * $text_number));
            break;
        default:
            exit('Error : wrong score!');
    }
}

function calculateGaoFei($level, $score, $text_number, $read_number)
{
    $gaofei = ceil($level * 0.1 * $read_number * 0.01 * 2 / 3 * $score * 0.2 * $text_number / 1000 * 10);
    if ($gaofei > 70) {
        $gaofei = 70;
    }
    return $gaofei;
}




//function send_email($email_address , $code){
////使用163邮箱服务器
//    $smtpserver = "smtp.163.com";
////163邮箱服务器端口
//    $smtpserverport = 25;
////你的163服务器邮箱账号
//    $smtpusermail = "Huanghao_feng@163.com";
////收件人邮箱
//    $smtpemailto = "{$email_address}";
////你的邮箱账号(去掉@163.com)
//    $smtpuser = "Huanghao_feng";//SMTP服务器的用户帐号
////你的邮箱密码
//    $smtppass = "Hao3732856"; //SMTP服务器的用户密码
////邮件主题
//    $mailsubject = "来译网注册";
////邮件内容
//    $mailbody = "非常感谢您注册来译网,你的验证码是:{$code}";
////邮件格式（HTML/TXT）,TXT为文本邮件
//    $mailtype = "TXT";
////这里面的一个true是表示使用身份验证,否则不使用身份验证.
//    $smtp = new smtp($smtpserver, $smtpserverport, false, $smtpuser, $smtppass);
////是否显示发送的调试信息
//    $smtp->debug = TRUE;
////发送邮件
//    return $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
//}